package com.zpark.dao.impl;
import com.zpark.dao.ISouHuDao;
import com.zpark.pojo.Ranking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
 * @author xiaoyu
 * @version 1.0 2022/9/27
 */

@Repository
public class SouHuDaoImpl implements ISouHuDao {

    //自动注入 jdbc 模板
    @Autowired
    private JdbcTemplate jdbc;

    @Override
    public List<Ranking> selectAll() {
        String sql = "select * from ranking";
        return jdbc.query(sql, new BeanPropertyRowMapper<>(Ranking.class));
    }

    @Override
    public Ranking selectById(Integer id) {
        String sql = "select * from ranking where id = ?";
        return jdbc.queryForObject(sql, new BeanPropertyRowMapper<>(Ranking.class), new Object[]{id});
    }

    @Override
    public Integer insert(Ranking ranking) {
        String sql = "insert into ranking(film_index, film_name, film_actor_name, film_feasibly) values(?,?,?,?)";
        return jdbc.update(sql, ranking.getFilmIndex(), ranking.getFilmName(), ranking.getFilmActorName(), ranking.getFilmFeasibly());
    }

    @Override
    public Integer updateById(Integer id, String fName) {
        String sql = "update ranking set fim_name = ? where id = ?";
        return jdbc.update(sql, fName, id);
    }

    @Override
    public Integer deleteById(Integer id) {
        String sql = "delete from ranking where id = ?";
        return jdbc.update(sql, id);
    }
}
